<template>
  <view v-if="goods_info.goods_name">
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" class="banner" :circular="true">
      <swiper-item v-for="(v, i) in goods_info.pics" :key="i" @click="handlePreview(i)">
        <view class="swiper-item">
          <image :src="v.pics_mid_url" mode=""></image>
        </view>
      </swiper-item>
    </swiper>
    <!-- 商品信息 -->
    <view class="goods-info">
      <view class="goods-body">
        <view class="goods-price">￥：{{ goods_info.goods_price }}</view>
        <view class="goods-collect">
          <uni-icons type="star" size="20"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="goods-name">{{ goods_info.goods_name }}</view>
    </view>
    <!-- 商品详情 -->
    <!-- 富文本渲染 -->
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    <!-- 底部商品导航 -->
    <view class="placeholder">
      <uni-goods-nav class="fixed-bottom" :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    onLoad(options) {
      this.goods_id = options.goods_id;
      this.getGoodsInfo()
    },
    computed: {
      ...mapGetters('car', ['totalCount'])
    },
    watch: {
      totalCount: {
        handler(newValue) {
          this.options[2].info = newValue
        },
        // 立即执行一次
        immediate: true,
        // deep: true
      }
    },
    data() {
      return {
        goods_id: "",
        goods_info: {},
        options: [{
          icon: 'headphones',
          text: '客服'
        }, {
          icon: 'shop',
          text: '店铺',
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    methods: {
      ...mapMutations('car', ['addToCarList']),
      onClick({ index }) {
        if(index === 2) {
          uni.switchTab({
            url: '/pages/car/car'
          })
        }
      },
      buttonClick({
        index
      }) {
        if (index === 0) {
          const data = {
            goods_id: this.goods_info.goods_id,
            goods_small_logo: this.goods_info.goods_small_logo,
            goods_price: this.goods_info.goods_price,
            goods_name: this.goods_info.goods_name,
            goods_count: 1,
            goods_state: true,
          }
          this.addToCarList(data)
        }
      },
      handlePreview(i) {
        // 调用 uni 预览图片效果
        uni.previewImage({
          // 图片列表数组
          urls: this.goods_info.pics.map(v => v.pics_big_url),
          // 当前索引
          current: i
        })
      },
      getGoodsInfo() {
        uni.request({
          url: 'https://www.uinav.com/api/public/v1/goods/detail?goods_id=' + this.goods_id,
          success: ({
            data
          }) => {
            const {
              message,
              meta
            } = data;
            if (meta.status !== 200) return uni.showToast({
              title: '请求失败',
              icon: 'error'
            });
            this.goods_info = message
            this.goods_info.goods_introduce = this.goods_info.goods_introduce.replace(/<img /g,
              `<img style="display: block;" `).replace(/webp/g, 'jpg')
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .banner {
    height: 400rpx;

    .swiper-item {
      width: 100%;
      height: 100%;

      image {
        width: 100%;
        height: 100%;
      }
    }

  }

  .goods-info {
    padding: 16rpx;
    background-color: white;

    .goods-name {
      font-size: 28rpx;
      font-weight: bold;
    }

    .goods-body {
      display: flex;
      justify-content: space-between;
      color: gray;
      margin-bottom: 10rpx;

      .goods-price {
        color: red;
        font-weight: bold;
      }

      .goods-collect {
        display: flex;
        flex-direction: column;
        align-items: center;
        font-size: 24rpx;
      }
    }
  }

  .placeholder {
    height: 50px;
  }

  .fixed-bottom {
    position: fixed;
    bottom: 0%;
    width: 100%;
  }
</style>
